﻿// --------------------------------
// <copyright file="CalculationEntryForm.cs" company="TRONSoft">
//     Copyright (c) TRONSoft.
// </copyright>
// <license>
//   This source code is subject to terms and conditions of the Microsoft
//   Public License. A copy of the license can be found in the License.html
//   file at the root of this distribution. If you cannot locate the
//   Microsoft Public License, please send an email to dlr@microsoft.com.
//   By using this source code in any fashion, you are agreeing to be bound 
//   by the terms of the Microsoft Public License. You must not remove this
//   notice, or any other, from this software.
// </license>
// <author>Tron</author>
// ---------------------------------

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace Actucal
{
    public partial class CalculationEntryForm : DockContent
    {
        public CalculationEntryForm()
        {
            InitializeComponent();

            ProductsService.CurrentProductChanged += ProductsService_CurrentProductChanged;
            AddCalculationForm.CalculationAdded += AddCalculationForm_CalculationAdded;
        }

        public event EventHandler<ArgumentEventArgs<string>> CurrentCalculationChanged;

        private void AddCalculationForm_CalculationAdded(object sender, EventArgs e)
        {
            AddCalculationForm frm = sender as AddCalculationForm;
            if (null != frm)
            {
                lbCalculations.Items.Add(frm.CalculationName);
            }
        }

        private void ProductsService_CurrentProductChanged(object sender, ArgumentEventArgs<Product> e)
        {
            LoadCalculations(e.Argument);
        }

        protected virtual void OnCurrentCalculationChanged(ArgumentEventArgs<string> e)
        {
            if (null != CurrentCalculationChanged)
                CurrentCalculationChanged(this, e);
        }

        private void miDelete_Click(object sender, EventArgs e)
        {
            if (lbCalculations.SelectedIndex != -1)
            {
                string calName = lbCalculations.SelectedItem.ToString();
                ProductsService.CurrentProduct.DeleteAttribute(calName);
                lbCalculations.Items.RemoveAt(lbCalculations.SelectedIndex);
            }
        }

        private void CalculationEntryForm_Load(object sender, EventArgs e)
        {
            LoadCalculations(ProductsService.CurrentProduct);
        }

        private void LoadCalculations(Product product)
        {
            lbCalculations.Items.Clear();
            if ((product != null ) && (product.Attributes != null))
            {
                foreach (KeyValuePair<string, string> pair in product.Attributes)
                {
                    lbCalculations.Items.Add(pair.Key);
                }
            }
        }

        private void lbCalculations_SelectedIndexChanged(object sender, EventArgs e)
        {
            OnCurrentCalculationChanged(new ArgumentEventArgs<string>(lbCalculations.SelectedItem.ToString()));
        }
    }
}